import { createSlice } from '@reduxjs/toolkit'
/**
 * 对于一个切片来说，需要暴露哪些东西？
 * 1. 默认暴露 reducer
 * 2. 分别暴露 actionCreator
 * 
 */
// 创建商品切片
const goodsSlice = createSlice({
    name: 'goods',
    initialState: {
        goodsList: []
    },
    reducers: {
        addGoods(state, { payload }) { //payload {gname:'华为',price:1999}
            console.log('goods payload: ', payload);
            // 添加商品
            state.goodsList = [...state.goodsList, {
                id: Math.random().toString(36).slice(2),
                ...payload
            }]
        }
    }
})
// 分别暴露actionCreator
export const {addGoods} = goodsSlice.actions;
// 默认暴露切片
export default goodsSlice.reducer;

